"use strict";
cc._RF.push(module, 'd8ac59lAOxEgowfQdmyMDED', 'BetPanelManage');
// scripts/bet/BetPanelManage.js

"use strict";

/*
聊天室投注界面管理中心
*/
cc.Class({
    extends: cc.Component,

    properties: {
        //幸运28面板列表
        fbXY28PanelList: cc.Prefab,
        //加拿大28面板列表
        fbJND28PanelList: cc.Prefab,
        //115面板列表(带乐选)
        fb115PanelList: cc.Prefab,
        //重庆时时彩
        fbCQSSCList: cc.Prefab,

        //支付	
        fbRechargePay: cc.Prefab,

        _lotteryID: "", //彩种ID
        _roomID: "", //房间ID
        _duration: 0.3, //投注列表效果时间
        _isAutoSel: false, //是否机选
        _lotteryList: [], //投注面板列表
        _isOpenInit: false //是否已经初始化投注面板

    },

    // use this for initialization
    onLoad: function onLoad() {
        this.initEvent();
        this._isAutoSel = false;
    },

    //初始化事件
    initEvent: function initEvent() {
        window.Notification.on("BET_ONCLOSE", function (data) {
            this.onCloseBet();
        }, this);

        window.Notification.on("BET_ONPAY", function (data) {
            this.payBtnCallback(data);
        }, this);

        window.Notification.on("BET_NEXTPAGE", function (data) {
            this.onNextPage(data);
        }, this);
    },

    //初始化信息
    init: function init(lotteryId, roomid) {
        this._lotteryID = lotteryId;
        this._roomID = roomid;

        if (this._lotteryList.length > 0) {
            cc.error("没有投注列表");
            return;
        }

        //添加投注界面
        this._lotteryList.push(this.fbXY28PanelList);
        this._lotteryList.push(this.fbJND28PanelList);
        this._lotteryList.push(this.fb115PanelList);
        this._lotteryList.push(this.fbCQSSCList);
    },

    //初始化投注面板
    initBetCollection: function initBetCollection() {
        var index = LotteryUtils.getPageByLotteryid(this._lotteryID.toString());
        if (this._lotteryList[index] !== null) {
            var betPanel = cc.instantiate(this._lotteryList[index]);
            if (betPanel != null) {
                betPanel.getComponent(betPanel.name).init(this._lotteryID);
                this.node.addChild(betPanel);
            }
        }
    },

    //打开投注面板
    onOpenInfo: function onOpenInfo() {
        if (!this._isOpenInit) {
            this._isOpenInit = true;
            this.initBetCollection();
        }
        this.showBet(this.node);
    },

    //关闭投注面板
    onCloseBet: function onCloseBet() {
        this.hideBet(this.node);
    },

    //显示投注界面
    showBet: function showBet(betNode) {
        betNode.opacity = 0;
        betNode.active = true;
        betNode.runAction(cc.sequence(cc.fadeTo(this._duration, 255), cc.callFunc(function () {}.bind(this), this)));
    },

    //隐藏投注界面
    hideBet: function hideBet(betNode) {
        betNode.runAction(cc.sequence(cc.fadeTo(this._duration, 0), cc.callFunc(function () {
            betNode.active = false;
        }.bind(this), this)));
    },

    //付款
    payBtnCallback: function payBtnCallback(data) {
        // cc.log("public pay 3");
        var dataRev = data;
        var recv = function (ret) {
            ComponentsUtils.unblock();
            var obj = ret.Data;

            if (ret.Code == 49) {
                ComponentsUtils.showTips("服务器维护中，到其他彩种试试手气吧!");
                return;
            }
            if (ret.Code === 0 || ret.Code === 90) {
                //预售中可以购买
                //对返回数据做安全处理
                if (!Utils.isEffective(obj)) {
                    return;
                }
                var isuseNum = obj.IsuseNum;
                var endTimeDate = Utils.getDateForStringDate(obj.EndTime.toString());
                var endTimeStamp = Date.parse(endTimeDate) / 1000;

                var recv0 = function (ret0) {
                    ComponentsUtils.unblock();
                    var timestamp = Date.parse(new Date()) / 1000;
                    var leftTimeStamp = endTimeStamp - parseInt(ret0.ServerTime / 1000);
                    if (leftTimeStamp <= 0) {
                        ComponentsUtils.showTips("当前期数已截止");
                        return;
                    } else {
                        var moneyTemp = LotteryUtils.moneytoClient(dataRev.money);
                        var dataRet = {
                            lotteryID: this._lotteryID,
                            lotteryMoney: moneyTemp,
                            isuseid: obj.IsuseNum,
                            endTime: obj.EndTime,
                            beginTime: obj.BeginTime,
                            baseData: dataRev.dataBase,
                            otherData: dataRev.otherBase,
                            buyType: dataRev.buyType,
                            betType: dataRev.betType
                        };
                        var quickPayCallback = function (ret) {
                            dataRet.lotteryMoney = ret.payMoney;
                            // cc.log("public pay 2");
                            this.doBalancePay(dataRet, ret);
                        }.bind(this);
                        var quickRechargePage = cc.instantiate(this.fbRechargePay);
                        var canvas = cc.find("Canvas");
                        quickRechargePage.getComponent(quickRechargePage.name).init(this._lotteryID, obj.IsuseNum, dataRev.money, dataRev.buyType, true, quickPayCallback);
                        canvas.addChild(quickRechargePage);
                    }
                }.bind(this);
                var data = {
                    Token: User.getLoginToken()
                };
                CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETSERVERTIME, data, recv0.bind(this), "POST");
                ComponentsUtils.block();
            } else {
                ComponentsUtils.showTips(ret.Msg);
                return;
            }
        }.bind(this);

        var data = {
            Token: User.getLoginToken(),
            LotteryCode: this._lotteryID
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETRECURRENTISUSE, data, recv.bind(this), "POST");
        ComponentsUtils.block();
    },

    //账户余额支付
    doBalancePay: function doBalancePay(dataBase, otherBase) {
        // cc.log("public pay 4");
        var betData = "";
        var istr = dataBase.baseData.substr(0, 1);
        if (istr != "[") {
            betData = "[" + dataBase.baseData + "]";
        } else {
            betData = dataBase.baseData;
        }

        if (dataBase.betType == DEFINE.BET_PAY_TYPE.SPECIAL) {
            var recv = function (ret) {
                ComponentsUtils.unblock();
                if (ret.Code === 0) {
                    this.onCloseBet();
                    ComponentsUtils.showTips("支付成功");
                    window.Notification.emit("moneyShow", null);
                } else {
                    ComponentsUtils.showTips(ret.Msg);
                }
            }.bind(this);

            var data = {
                Token: User.getLoginToken(),
                UserCode: User.getUserCode(),
                LotteryCode: dataBase.lotteryID,
                BuyType: dataBase.buyType, //0代购 1追号 2跟单
                Amount: dataBase.lotteryMoney,
                IsuseNum: dataBase.isuseid,
                RoomCode: this._roomID,
                BetData: betData,
                BeginTime: dataBase.beginTime,
                EndTime: dataBase.endTime
            };
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.SPECIALBET, data, recv.bind(this), "POST");
            ComponentsUtils.block();
        } else {
            var recv = function (ret) {
                ComponentsUtils.unblock();
                if (ret.Code === 0) {
                    this.onCloseBet();
                    ComponentsUtils.showTips("支付成功");
                    window.Notification.emit("moneyShow", null);
                } else {
                    ComponentsUtils.showTips(ret.Msg);
                }
            }.bind(this);

            var data = {
                Token: User.getLoginToken(),
                IsuseNum: dataBase.isuseid,
                BeginTime: dataBase.beginTime,
                EndTime: dataBase.endTime,
                RoomCode: this._roomID,
                BetData: betData,
                ChaseData: dataBase.otherData,
                UserCode: User.getUserCode(),
                LotteryCode: dataBase.lotteryID,
                BuyType: dataBase.buyType, //0代购 1追号 2跟单
                Amount: dataBase.lotteryMoney,
                Coupons: "",
                PaymentType: otherBase.paymentType, //1.余额彩豆购彩券支付 2.余额支付 3.购彩券支付 4.彩豆支付 5.余额和购彩券支付 6.余额和彩豆支付 7.彩豆和购彩券支付
                Gold: 0 //彩豆支付
            };

            CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.REQUESTBET, data, recv.bind(this), "POST");
            ComponentsUtils.block();
        }
    },

    onDestroy: function onDestroy() {
        //  cc.log("public pay 5");
        window.Notification.offType("BET_ONPAY");
        window.Notification.offType("BET_ONCLOSE");
        window.Notification.offType("BET_NEXTPAGE");
    }

});

cc._RF.pop();